from datetime import datetime, time

from sqlalchemy import asc, delete, desc, select
from sqlalchemy.ext.asyncio import AsyncSession

from module_basics.entity.do.log_do import ComparativeContrastLog
from module_basics.entity.vo.log_vo import ContrastLogPageQueryModel, ContrastLogModel
from utils.common_util import SnakeCaseUtil
from utils.page_util import PageUtil


class ContrastLogDao:
    """
    对比日志数据库操作层
    """

    @classmethod
    async def get_contrast_log_list(cls, db: AsyncSession, query_object: ContrastLogPageQueryModel,
                                    is_page: bool = False):
        """
        根据查询参数获取对比日志的列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 对比日志列表信息对象
        """
        if query_object.is_asc == 'ascending':
            order_by_column = asc(
                getattr(ComparativeContrastLog, SnakeCaseUtil.camel_to_snake(query_object.order_by_column), None))
        elif query_object.is_asc == 'descending':
            order_by_column = desc(
                getattr(ComparativeContrastLog, SnakeCaseUtil.camel_to_snake(query_object.order_by_column), None)
            )
        else:
            order_by_column = desc(ComparativeContrastLog.operation_time)
        query = (
            select(ComparativeContrastLog)
            .where(
                ComparativeContrastLog.source_file_name.like(
                    f'%{query_object.source_file_name}%') if query_object.source_file_name else True,
                ComparativeContrastLog.target_file_name.like(
                    f'%{query_object.target_file_name}%') if query_object.target_file_name else True,
                ComparativeContrastLog.operation_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_time and query_object.end_time
                else True,
            )
            .distinct()
            .order_by(order_by_column)
        )
        operation_log_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return operation_log_list

    @classmethod
    async def add_contrast_log_dao(cls, db: AsyncSession, contrast_log: ContrastLogModel):
        """
        新增对比日志数据库操作

        :param db: orm对象
        :param contrast_log: 操作日志对象
        :return: 新增校验结果
        """
        db_operation_log = ComparativeContrastLog(**contrast_log.model_dump())
        db.add(db_operation_log)
        await db.flush()

        return db_operation_log

    @classmethod
    async def delete_contrast_log_dao(cls, db: AsyncSession, contrast_log: ContrastLogModel):
        """
        删除操作日志数据库操作

        :param db: orm对象
        :param contrast_log: 操作日志对象
        :return:
        """
        await db.execute(delete(ComparativeContrastLog).where(ComparativeContrastLog.id.in_([contrast_log.id])))

    @classmethod
    async def clear_contrast_log_dao(cls, db: AsyncSession):
        """
        清除操作日志数据库操作

        :param db: orm对象
        :return:
        """
        await db.execute(delete(ComparativeContrastLog))
